<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      // 参数是函数或者返回值是函数   - 高阶函数
      // 将myReduce遍历方法挂载在原型对象上  
      /* cb 回调函数
      (item,index,arr)=>{
        console.log(item,index,arr)
      }
      */
     Array.prototype.myReduce = function(cb){
      let sum = 0
      for(let i = 0;i<this.length;i++){
        // console.log(this); //数组
        sum+=this[i]  //将数组里的每一项加等赋值给sum
      }
       return sum
     }
      const arr = [1,2,4,15,8]
      const arr1 =  arr.myReduce((prev,item)=>{
        prev+=item
      },0)
      console.log(arr1);
    </script>
  </body>
</html>